package com.yunji.order.command.retund.impl;

import com.yunji.order.command.retund.AbsRefundCommand;
import com.yunji.order.domain.OrderDO;
import com.yunji.order.domain.RefundDO;
import com.yunji.order.domain.RefundDetailDO;
import com.yunji.order.dto.cmd.RefundCreateCommandDTO;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2025/3/8 09:55
 * @description : TODO
 **/
@Component
public class RefundCreateCommand extends AbsRefundCommand<RefundCreateCommandDTO, RefundDO> {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public RefundDO execute(RefundCreateCommandDTO cmd) {
        RefundDO refundDO = cmd.getRefundDO();
        refundService.createRefund(refundDO);
        List<RefundDetailDO> detailList = cmd.getDetailList();
        detailList.forEach(detail -> {
            detail.setRefundId(refundDO.getRefundId());
            detail.setRefundCode(refundDO.getRefundCode());
        });
        OrderDO updateOrder = new OrderDO();
        updateOrder.setOrderId(refundDO.getOrderId());
        updateOrder.setRefund(1);
        orderService.updateOrder(updateOrder);
        refundService.createRefundDetail(detailList);
        return refundDO;
    }
}
